<?php

namespace teach\service;

use Yii;

class Notify
{
    /**
     * 查看支付表中是否已经存在记录
     * @param $condition
     * @return false|null|string
     */
    public function payCount($condition)
    {
        $sql = "SELECT COUNT(*) FROM mall_pay WHERE {$condition}";
        return Yii::$app->db->createCommand($sql)->queryScalar();
    }

    /**
     * 将订单改为已经支付状态
     * @param $parameter
     * @param $parameters
     * @return int
     */
    public function orderPay($parameter, $parameters)
    {
        $sql = "UPDATE mall_order SET {$parameter} WHERE {$parameters}";
        return Yii::$app->db->createCommand($sql)->execute();
    }

    /**
     * 充值订单改为已支付成功
     * @param $parameter
     * @param $parameters
     * @return int
     */
    public function rechargePay($parameter, $parameters)
    {
        $sql = "UPDATE mall_recharge_record SET {$parameter} WHERE {$parameters}";
        return Yii::$app->db->createCommand($sql)->execute();
    }

    /**
     * 插入一条微信支付成功的回调
     * @param $map
     * @return int
     */
    public function payInsert($map)
    {
        return Yii::$app->db->createCommand()->insert('mall_pay', $map)->execute();
    }

    /**
     * 插入一条充值微信成功的回调记录
     * @param $map
     * @return int
     */
    public function rechargeInsert($map)
    {
        return Yii::$app->db->createCommand()->insert('mall_recharge_pay', $map)->execute();
    }

    /**
     * 查询一条汇总订单
     * @param $where
     * @return array|false
     */
    public function orderOne($where)
    {
        $sql = "SELECT * FROM mall_order WHERE {$where}";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    /**
     * 查找其中一个用户
     * @param $andWhere
     * @return array|false
     */
    public function userOne($andWhere)
    {
        $sql = "SELECT * FROM edu_user WHERE {$andWhere}";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    /**
     * 获取商家的规则
     * @param $filterWhere
     * @return array|false
     */
    public function ruleOne($filterWhere)
    {
        $sql = "SELECT * FROM mall_distribution WHERE {$filterWhere}";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    /**
     * 精确查找订单号
     * @param $out_trade_no
     * @return array|false
     */
    public function getOrder($out_trade_no)
    {
        $sql = "SELECT * FROM mall_order WHERE out_trade_no = '{$out_trade_no}'";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    /**
     * 减去用户的支票
     * @param $uid
     * @param $recharge
     * @return int
     */
    public function subRecharge($uid, $recharge)
    {
        $sql = "UPDATE edu_user SET recharge = recharge - {$recharge} WHERE id = {$uid}";
        return Yii::$app->db->createCommand($sql)->execute();
    }

    /**
     * 充值订单数量查询
     * @param $condition
     * @return false|null|string
     */
    public function rechargeCount($condition)
    {
        $sql = "SELECT COUNT(*) FROM mall_recharge_pay WHERE {$condition}";
        return Yii::$app->db->createCommand($sql)->queryScalar();
    }

    /**
     * 给用户或者商家加入相应的充值金额
     * @param $openid
     * @param $total_fee
     * @return int
     */
    public function userRecharge($openid, $total_fee)
    {
        $sql = "UPDATE edu_user SET recharge = recharge + {$total_fee}, score_total = score_total + {$total_fee} WHERE openid = '{$openid}'";
        return Yii::$app->db->createCommand($sql)->execute();
    }

    /**
     * 查找拼团订单
     * @param $where
     * @return false|null|string
     */
    public function payAssembleCount($where)
    {
        $sql = "SELECT COUNT(*) FROM mall_assemble_pay WHERE {$where}";
        return Yii::$app->db->createCommand($sql)->queryScalar();
    }

    /**
     * 将拼团订单改为已经支付
     * @param $andWhere
     * @param $filter
     * @return int
     */
    public function orderAssemblePay($andWhere, $filter)
    {
        $sql = "UPDATE mall_assemble_order SET {$andWhere} WHERE {$filter}";
        return Yii::$app->db->createCommand($sql)->execute();
    }

    /**
     * 插入回调订单数据
     * @param $map
     * @return int
     */
    public function payAssembleInsert($map)
    {
        return Yii::$app->db->createCommand()->insert('mall_assemble_pay', $map)->execute();
    }

    /**
     * 查询一条拼团订单
     * @param $where
     * @return array|false
     */
    public function assembleOrderOne($where)
    {
        $sql = "SELECT * FROM mall_assemble_order WHERE {$where}";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    /**
     * 查找一条拼团产品看看是否要进入提成
     * @param $pid
     * @return array|false
     */
    public function productAssembleOne($pid)
    {
        $sql = "SELECT * FROM mall_assemble_product WHERE id = {$pid}";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }
}